﻿using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using MobileWebsiteGenerator.Entities;
using MobileWebsiteGenerator.Mapping;

namespace MobileWebsiteGenerator
{
    public class Mobile_appContext : DbContext
    {
        static Mobile_appContext()
        {
            Database.SetInitializer<Mobile_appContext>(new DatabaseInitializer());
        }

        public DbSet<Customer> Customers { get; set; }
        public DbSet<Page> Pages { get; set; }
        public DbSet<pictureGallery> pictureGalleries { get; set; }
        public DbSet<ThemeDetail> ThemeDetails { get; set; }
        public DbSet<ThemePageREL> ThemePageRELs { get; set; }
        public DbSet<ThemePictureREL> ThemePictureRELs { get; set; }
        public DbSet<Website> Websites { get; set; }
        public DbSet<WebthemeRel> WebthemeRels { get; set; }
        public DbSet<WebsitePages> WebsitePages { get; set; }
        public DbSet<CustomerWebsiteREL> CustomerWebsiteRels { get; set; }
        public DbSet<Metatag> Metatags { get; set; }
        public DbSet<WebMetaREL> WebMetaRELs { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Configurations.Add(new CustomerMap());
            modelBuilder.Configurations.Add(new PageMap());
            modelBuilder.Configurations.Add(new pictureGalleryMap());
            modelBuilder.Configurations.Add(new ThemeDetailMap());
            modelBuilder.Configurations.Add(new ThemePageRELMap());
            modelBuilder.Configurations.Add(new ThemePictureRELMap());
            modelBuilder.Configurations.Add(new WebsiteMap());
            modelBuilder.Configurations.Add(new WebthemeRelMap());
            modelBuilder.Configurations.Add(new CustomerWebsiteMap());
            modelBuilder.Configurations.Add(new MetaTagMap());
            modelBuilder.Configurations.Add(new WebMetaRelMap());
        }

        public class DatabaseInitializer : CreateDatabaseIfNotExists<Mobile_appContext>
        {
            protected override void Seed(Mobile_appContext context)
            {
                var website = new Website { IsContactusPage = true, IsPictureGallery = true, visitorcount = 0, web_url = "MyCompany" };
                context.Websites.Add(website);
                context.SaveChanges();
                //Add themes


                var theme = new[] {
                    new ThemeDetail{backgroundColor="#FFFFFF",backgroundImage="",Footertext="An IT Company",Headercolor="#000",HeaderImage="",HeaderText="SHIVAM IT Solution",Image="1.png",Swatch="#7E2217, #254117, #7E2217"},
                    new ThemeDetail{backgroundColor="#a3796f",backgroundImage="",Footertext="An Art School",Headercolor="#2e0b11",HeaderImage="",HeaderText="JSON Acadamy",Image="2.png",Swatch="#800517, #C34A2C,#5C5858"},
                    new ThemeDetail{backgroundColor="#d74c50",backgroundImage="",Footertext="Indian Tourist",Headercolor="#000",HeaderImage="",HeaderText="Incredible India",Image="3.png",Swatch="#4E4E00, #000000, #4E4E00"},
                    new ThemeDetail{backgroundColor="#FFFFFF",backgroundImage="",Footertext="Footer",Headercolor="#000",HeaderImage="",HeaderText="My businessCompany",Image="4.png",Swatch="#41627E, #B5BCE5, #FFFFFF"},
                    new ThemeDetail{backgroundColor="#736F6E",backgroundImage="",Footertext="Footer",Headercolor="#800517",HeaderImage="",HeaderText="My businessCompany",Image="5.png",Swatch="#800517, #C34A2C,#5C5858"},
                    new ThemeDetail{backgroundColor="#000",backgroundImage="212.png",Footertext="Footer",Headercolor="#000",HeaderImage="",HeaderText="My businessCompany",Image="11.png",Swatch="#000000, #CCCCCC,#000000"},
                    new ThemeDetail{backgroundColor="#120712",backgroundImage="",Footertext="Footer",Headercolor="#463e41",HeaderImage="",HeaderText="My businessCompany",Image="8.png",Swatch="#A0C544, #C34A2C,#E66C2C"},
                    new ThemeDetail{backgroundColor="#E55451",backgroundImage="back1.jpg",Footertext="Indian Tourist",Headercolor="#000000",HeaderImage="",HeaderText="Incredible india",Image="9.png",Swatch="#000000, #C9C299,#000000"},
                    new ThemeDetail{backgroundColor="",backgroundImage="21.jpg",Footertext="Footer",Headercolor="#000",HeaderImage="",HeaderText="My businessCompany",Image="10.png",Swatch="#000,#ccc,#eee"},
                };




                //Add Pages
                var pages = new[]{
                  new Page{title="Page1",EContent="<h1>Page1 Content</h1>"},
                  new Page{title="Page2",EContent="<h1>Page2 Content</h1>"},
                  new Page{title="Page3",EContent="<h1>Page3 Content</h1>"},
                  new Page{title="ContactUs",EContent="<h1>Contact Us</h1>"}
                };


                //Add pictures
                var pictures = new[]{
                  new pictureGallery{ImagePath="image1.png",Size="120x120"},
                  new pictureGallery{ImagePath="image2.png",Size="120x120"},
                  new pictureGallery{ImagePath="image3.png",Size="120x120"}
                };

                foreach (var p in pages)
                {
                    context.Pages.Add(p);
                    context.SaveChanges();
                }
                foreach (var pic in pictures)
                {
                    context.pictureGalleries.Add(pic);
                    context.SaveChanges();
                }
                int k = 0, i = 0, m = 0;
                foreach (var item in theme)
                {
                    context.ThemeDetails.Add(item);
                    context.SaveChanges();
                    //WebTheme relation
                    WebthemeRel obj = new WebthemeRel();
                    obj.Web_id = website.web_id;
                    obj.Web_theme_id = ++k;
                    obj.Theme_id = item.theme_id;

                    context.WebthemeRels.Add(obj);
                    context.SaveChanges();
                    //pic theme relation 

                    foreach (var pic in pictures)
                    {
                        ThemePictureREL objpic = new ThemePictureREL();
                        objpic.Picture_id = pic.picture_id;
                        objpic.Theme_id = item.theme_id;
                        objpic.Theme_picture_id = ++i;
                        context.ThemePictureRELs.Add(objpic);
                        context.SaveChanges();
                    }

                    //page theme relation
                    foreach (var p in pages)
                    {
                        ThemePageREL tpr = new ThemePageREL();
                        tpr.Page_id = p.page_id;
                        tpr.Theme_id = item.theme_id;
                        tpr.Theme_Page_id = ++m;
                        context.ThemePageRELs.Add(tpr);
                        context.SaveChanges();
                    }

                    //Website pages content




                }

                var webPages = new[]{
                 new WebsitePages{ID=1,HTitle="מדד",ETitle="Index",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                 new WebsitePages{ID=2,HTitle="מי אנחנו",ETitle="About Us",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                 new WebsitePages{ID=3,HTitle="תכונות",ETitle="Features",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                 new WebsitePages{ID=4,HTitle="בסיסי",ETitle="Basic",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                 new WebsitePages{ID=5,HTitle="אמצע",ETitle="Middle",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                 new WebsitePages{ID=6,HTitle="מתקדם",ETitle="Advanced",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"},
                  new WebsitePages{ID=7,HTitle="צור קשר",ETitle="Contact Us",EContent="Some text goes here",HContent="הטקסט חלק הולך כאן"}
                };
                foreach (var witem in webPages)
                {
                    context.WebsitePages.Add(witem);
                    context.SaveChanges();
                }

            }
        }
    }
}

